# -- coding: utf-8 --
"""
# [Example] Miahx© 交互框架信息表示模型的 DDS 消息收发示例

**模块：MIAHX© / 框架基础服务层（FFST） / 交互框架信息表示模型（IFIRM）**

Version: 0.4.10,
Last updated: October 2, 2024

## Contributor(s)
* YAO Naiming <[naiming@iscas.ac.cn](mailto:naiming@iscas.ac.cn)>

## Change log
### v0.4.10
- .2: [更新] 适配新接口
### v0.4.6
- .10: [新增] 初始实现

---
Copyright © 2024-2025 MIAHX Contributors, ISCAS, and ASIC. All Rights Reserved.
Licensed under Apache-2.0.
"""

import threading
import time

from miahx_support.dds.fastdds import *

import launcher


def vc_writer(p: Participant, e: threading.Event):
    writer = BngVehicleControlWriter(p)
    while not e.is_set():
        writer.write(vid='EGO', mode=EBngDrivingMode.CHASING)
        time.sleep(1)


def vc_reader(p: Participant, e: threading.Event):
    def on_data_available(data):
        hdr = data.header()
        dm = hdr.domain()
        src = hdr.source()
        log.info(f'[{dm}::{src}::BngVehicleControlReader] vid = `{data.vid()}`')
        log.info(f'[{dm}::{src}::BngVehicleControlReader] mode = `{EBngDrivingMode(data.mode())}`')

    with BngVehicleControlReader(p, on_data_listeners=[on_data_available]) as reader:
        log.success(f'[BngVehicleControlReader] Topic: {reader.topic_name}')
        reader.wait(e)


if __name__ == '__main__':
    log = launcher.log
    event = threading.Event()
    participant = Participant()
    launcher.run(lambda: (event, [
        threading.Thread(target=vc_writer, args=(participant, event), daemon=False),
        threading.Thread(target=vc_reader, args=(participant, event), daemon=False),
    ]))
